﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace JustTypeUI.Data
{
    /// <summary>
    /// Store recents task list and provides undo and redo commmands
    /// 
    /// Main Author: Han Yu
    /// </summary>
    public class History
    {
        Stack<List<Task>> undotask;
        Stack<List<Task>> redotask;
        Stack<List<Task>> undodone;
        Stack<List<Task>> redodone;

        public History()
        {
            undotask = new Stack<List<Task>>();
            redotask = new Stack<List<Task>>();
            undodone = new Stack<List<Task>>();
            redodone = new Stack<List<Task>>();
        }
        private List<Task> Copy(List<Task> x)
        {
            List<Task> temp = new List<Task>();
            for (int i = 0; i < x.Count; i++)
            {
                temp.Add(x[i].Copy());
            }
            return temp;
        }
        public void Put(List<Task> x,List<Task> y)
        {
            List<Task> temp = Copy(x);
            List<Task> temp1 = Copy(y);
            undotask.Push(temp);
            undodone.Push(temp1);
            redotask.Clear();
            redodone.Clear();
        }

        public Boolean GoBack(ref List<Task> x, ref List<Task> y)
        {
            if (undotask.Count > 1 && undodone.Count > 1)
            {
                redotask.Push(Copy(undotask.Peek()));
                redodone.Push(Copy(undodone.Peek()));
                undotask.Pop();
                undodone.Pop();
                x = Copy(undotask.Peek());
                y = Copy(undodone.Peek());
                return true;
            }
            else 
                return false;
        }
        public Boolean GoFoward(ref List<Task> x, ref List<Task> y)
        {
            if (redotask.Count != 0 && redodone.Count != 0)
            {
                
                x = Copy(redotask.Peek());
                y = Copy(redodone.Peek());
                undotask.Push(redotask.Pop());
                undodone.Push(redodone.Pop());
                return true;
            }
            else
                return false;
        }
    }
}
